Choices of packages

ggvis

Built on the javascript library vega

data("economics", package = "ggplot2")
ggvis(economics, x=~date, y=~psavert)

economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points() %>%
  layer_smooths()

Interactivity

The coolest thing about ggvis is that plot parameters don’t need to be static: they can be interactive

economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points() %>%
  layer_smooths(
    span = input_slider(0.2, 1)
  )

Attention grabber

Your turn

The values provided for the slider (0.2, 1) look like they are not ideal. Change slider bounds so that they are more appropriate for this data.

economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points(opacity := 0.5) %>%
  layer_smooths(
    stroke := "red",
    span = input_slider(0.1, 0.8)
  )

Labels

all_values <- function(x) {
  if (is.null(x)) return(NULL)
  paste0(names(x), ": ", format(x), collapse = "<br />")
}

economics %>%
  ggvis(~unemploy, ~psavert) %>%
  layer_points() %>%
  add_tooltip(all_values, "hover") 

Checkbox

model_type <- input_checkbox(label = "Use loess curve",
  map = function(val) if(val) "loess" else "lm")
economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points() %>%
    layer_model_predictions(model = model_type)

Your turn

library(MASS) # for rlm
model_type <- input_radiobuttons(
  choices = c("LOESS" = "loess", "LM" = "lm", "Robust" = "rlm"),
  selected = "loess",
  label = "Model type")
economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points(opacity := 0.5) %>%
    layer_model_predictions(model = model_type, stroke := "red")

plotly

The plotly package in R builds on the ggplot2 package, adding interactive elements to these plots. It translates plots to javascript.

plot_ly(economics, x = date, y = unemploy / pop)

Or using ggplot2

ggplot(data=economics, aes(x = date, y = unemploy / pop)) +  
        geom_point() + geom_line()

ggplotly()

Still a work in progress

library(GGally)
p <- ggpairs(economics[,3:6])
ggplotly(p)
data(canada.cities, package = "maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
  borders(regions = "canada") +
  coord_equal() +
  geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2)
 ggplotly(viz)

Return to the RNA-Seq data

library(edgeR)
coty <- read_delim("./data/GSE61857_Cotyledon_normalized.txt.gz",
  delim="\t", col_types="cddddddddd", 
  col_names=c("ID", "C_S1_R1", "C_S1_R2", "C_S1_R3", 
  "C_S2_R1", "C_S2_R2", "C_S2_R3", "C_S3_R1", "C_S3_R2", "C_S3_R3"),
  skip=1)
coty <- as.data.frame(coty)
d <- DGEList(counts = coty[,2:7], 
  group = c(rep("S1", 3), rep("S2", 3)), 
  genes = coty[,1])
d <- calcNormFactors(d)
d <- estimateCommonDisp(d)
d <- estimateTagwiseDisp(d)
d <- estimateTrendedDisp(d)
de <- exactTest(d, pair=c("S1", "S2"), dispersion = "trended")
sig.tab <- de$table
sig.tab$genes <- coty$ID
sig.tab <- dplyr::filter(sig.tab, PValue < 0.01)
sig.tab <- merge(sig.tab, coty[,1:7], by.x="genes", by.y="ID")
ggscatmat(sig.tab, columns=5:10, alpha=0.1)

p <- ggplot(sig.tab, aes(x=C_S1_R2, y=C_S2_R1, label=genes)) +
  geom_point(alpha=0.1) 
ggplotly(p)

With a large data set it can be slow!

Example, using Australian elections

library(eechidna)
launchApp(
  age = c("Age20_24", "Age85plus"),
  religion = c("Christianity", "Catholic", "NoReligion"),
  other = c("Unemployed", "Population", "MedianIncome")
)

Your turn

Resources

Share and share alike

This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/ 3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.